package occ.service
{
  import flash.data.*;
  import flash.events.*;
  import flash.events.EventDispatcher;
  import flash.events.IEventDispatcher;
  
  import model.*;
  
  import mx.collections.ArrayCollection;
  import mx.utils.*;
  
  import utils.*;
  
  public class GetSiteTypesDAO extends EventDispatcher
  {
    public static const COMPLETE:String="GetSiteTypeDAOIsComplete";
    
    public function GetSiteTypesDAO(target:IEventDispatcher=null)
    {
      super(target);
    }
    
    public function getSiteTypes():void
    {
      trace("GetSiteTypesDAO getSiteTypes");
      sql=" select st.site_type_id, st.site_type_name, st.daily_rate_week, "+
          "        st.season_rate, st.default_guest_count, st.add_guest_rate, "+ 
          "        st.daily_rate_weekend, st.active_flag, st.site_type_descr, "+
          "        s.site_id, s.site_name, s.site_descr "+
          " from site_types st left outer join sites s "+
          " on st.site_type_id = s.site_type_id "+
          " order by st.site_type_id  ";
      stmt = new SQLStatement();
			stmt.sqlConnection = DatabaseManager.getConnection();
			
			stmt.addEventListener( SQLErrorEvent.ERROR, onSQLError );
			stmt.addEventListener( SQLEvent.RESULT, onGetSiteTypes );
			stmt.text = sql;
			stmt.execute();
    }
    
    private function onGetSiteTypes(event:SQLEvent):void
    {
      trace("onGetSiteTypes");
      
      var result:SQLResult = stmt.getResult();
      trace("look at result");
      trace(ObjectUtil.toString(result));
			siteTypes= new ArrayCollection();
      try{
        sqlData = result.data;
        for(var i:int=0;i<sqlData.length;i++){
			  trace("in the loop " +  (sqlData[i] as Object).site_type_name)
			  var o:SiteTypeVO = new SiteTypeVO();
			  o.siteTypeId = (sqlData[i] as Object).site_type_id;
			  o.siteTypeName = (sqlData[i] as Object).site_type_name;
			  o.siteTypeDescr = (sqlData[i] as Object).site_type_descr;
			  o.dailyRateWeek=(sqlData[i] as Object).daily_rate_week;
        o.seasonRate=(sqlData[i] as Object).season_rate;
        o.defaultGuestCount=(sqlData[i] as Object).default_guest_count;
        o.addGuestRate=(sqlData[i] as Object).add_guest_rate;
        o.dailyRateWeekend=(sqlData[i] as Object).daily_rate_weekend;
        o.activeFlag=(sqlData[i] as Object).active_flag;
        o.rateDescr= "Weekends: $" + o.dailyRateWeekend + " NonWeekends: $" + o.dailyRateWeek + " Additional Guest: $" + o.addGuestRate + " Seasonal: $" + o.seasonRate;
			  siteTypes.addItem(o);
			}
      }catch(e:Error){}
      
			
			 trace("siteTypes: " + siteTypes.length);
			dispatchEvent(new Event(COMPLETE));
			 
    }
    
    private function onSQLError( event:SQLErrorEvent ):void
		{
			var errorString:String = "SQL Error: " + event.error.errorID + "nDetails: " + event.error.details + "nDetail Arguments: " +event.error.detailArguments;
 			throw new Error(errorString);
		}
    
    
  }
}